{extend name="../../base/view/common/base" /}
{block name="style"}
{include file="/approve/add_style" /}
{/block}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	<h3 class="pb-3">请假申请</h3>
	{eq name="$id" value="0"}
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray-2">请假类型<font>*</font></td>
			<td colspan="5">
				<select name="detail_type" lay-verify="required" lay-reqText="请选择请假类型">
				  <option value="">--请选择--</option>
				  <option value="1">事假</option>
				  <option value="2">年假</option>
				  <option value="3">调休假</option>
				  <option value="4">病假</option>
				  <option value="5">婚假</option>
				  <option value="6">丧假</option>
				  <option value="7">产假</option>
				  <option value="8">陪产假</option>
				  <option value="9">其他</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">开始时间<font>*</font></td>
			<td>
				<input id="start_time_a" name="start_time_a" autocomplete="off" class="layui-input select-time" value="" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td>
				<input id="start_time_b" name="start_time_b" autocomplete="off" class="layui-input select-datetime" value="" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td class="layui-td-gray">结束时间<font>*</font></td>
			<td>
				<input id="end_time_a" name="end_time_a" autocomplete="off" class="layui-input select-time" value="" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td>
				<input type="hidden" name="duration" value="">
				<input id="end_time_b" name="end_time_b" autocomplete="off" class="layui-input select-datetime" value="" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">请假工时</td>
			<td colspan="2" id="hours">-</td>
			<td class="layui-td-gray">请假天数</td>
			<td colspan="2" id="days">-</td>
		</tr>
		<tr>
			<td class="layui-td-gray">请假事由<font>*</font></td>
			<td colspan="5">
				<textarea name="content" placeholder="请输入请假事由" class="layui-textarea" lay-verify="required" lay-reqText="请输入请假事由"></textarea>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray"><div class="layui-input-inline">附件</div> <div class="layui-input-inline"><button type="button" class="layui-btn layui-btn-xs" id="upFile"><i class="layui-icon"></i></button></div></td>
			<td colspan="5" style="line-height:inherit">
				<div class="layui-row" id="fileList">
					<input type="hidden" name="file_ids" value="">
				</div>
			</td>
		</tr>
	</table>
	{else/}
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">请假类型<font>*</font></td>
			<td colspan="5">
				<select name="detail_type" lay-verify="required" lay-reqText="请选择请假类型">
				  <option value="">--请选择--</option>
				  <option value="1" {eq name="$detail.detail_type" value="1"}selected=""{/eq}>事假</option>
				  <option value="2" {eq name="$detail.detail_type" value="2"}selected=""{/eq}>年假</option>
				  <option value="3" {eq name="$detail.detail_type" value="3"}selected=""{/eq}>调休假</option>
				  <option value="4" {eq name="$detail.detail_type" value="4"}selected=""{/eq}>病假</option>
				  <option value="5" {eq name="$detail.detail_type" value="5"}selected=""{/eq}>婚假</option>
				  <option value="6" {eq name="$detail.detail_type" value="6"}selected=""{/eq}>丧假</option>
				  <option value="7" {eq name="$detail.detail_type" value="7"}selected=""{/eq}>产假</option>
				  <option value="8" {eq name="$detail.detail_type" value="8"}selected=""{/eq}>陪产假</option>
				  <option value="9" {eq name="$detail.detail_type" value="9"}selected=""{/eq}>其他</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">开始时间<font>*</font></td>
			<td>
				<input id="start_time_a" name="start_time_a" autocomplete="off" class="layui-input select-time" value="{$detail.start_time_a}" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td>
				<input id="start_time_b" name="start_time_b" autocomplete="off" class="layui-input select-datetime" value="{$detail.start_time_b}" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td class="layui-td-gray">结束时间<font>*</font></td>
			<td>
				<input id="end_time_a" name="end_time_a" autocomplete="off" class="layui-input select-time" value="{$detail.end_time_a}" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
			<td>
				<input type="hidden" name="duration" value="{$detail.duration}">
				<input id="end_time_b" name="end_time_b" autocomplete="off" class="layui-input select-datetime" value="{$detail.end_time_b}" readonly lay-verify="required" placeholder="请选择时间" lay-reqText="请选择时间">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">请假工时</td>
			<td colspan="2" id="hours">共{$detail.duration}工时</td>
			<td class="layui-td-gray">请假天数</td>
			<td colspan="2" id="days">共{$detail.days}天,{$detail.hours}小时</td>
		</tr>
		<tr>
			<td class="layui-td-gray">请假事由<font>*</font></td>
			<td colspan="5">
				<textarea name="content" placeholder="请输入请假事由" class="layui-textarea" lay-verify="required" lay-reqText="请输入请假事由">{$detail.content}</textarea>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray"><div class="layui-input-inline">附件</div> <div class="layui-input-inline"><button type="button" class="layui-btn layui-btn-xs" id="upFile"><i class="layui-icon"></i></button></div></td>
			<td colspan="5" style="line-height:inherit">
				<div class="layui-row" id="fileList">
					<input type="hidden" name="file_ids" value="{$detail.file_ids}">
					{notempty name="$detail.file_ids"}
					{volist name="$detail.fileArray" id="vo"}
					<div class="layui-col-md4" id="uploadImg{$vo.id}">
						<div class="file-card">
							<i class="file-icon iconfont icon-renwuguanli"></i>
							<div class="file-title">{$vo.name}</div>
							<div class="file-tool">
								<a href="{$vo.filepath}" download="{$vo.name}" target="_blank" title="下载查看"><i class="iconfont icon-shujudaoru blue"></i></a>
								<i class="btn-delete iconfont icon-shanchu red" data-id="{$vo.id}" title="删除"></i>
							</div>
						</div>
					</div>
					{/volist}
					{/notempty}
				</div>
			</td>
		</tr>
	</table>
	{/eq}
	{include file="/approve/add_flow" /}
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
/**
 * 工时计算
  */
function leaveTime() {
	var data_1 = $('#start_time_a').val();
	var data_1_1 = $('#start_time_b').val();
	var data_2 = $('#end_time_a').val();
	var data_2_1 = $('#end_time_b').val();
	if(data_1=='' || data_1_1=='' || data_2=='' || data_2_1==''){
		return 0;
	}	

    var start_date = data_1+' '+data_1_1; 
    var end_date = data_2+' '+data_2_1; 
	var start = new Date(start_date.replace(/-/g,'/'));
	var end = new Date(end_date.replace(/-/g,'/'));
    var startHour = start.getHours(); //16
    var startMin = start.getMinutes(); 
    var endHour = end.getHours();
	var endMin = end.getMinutes(); 
	
    var startDate = start.setHours(0, 0, 0);
    var endDate = end.setHours(0, 0, 0);
	if(endDate<startDate){
		return 0;
	}
    var dayNum = parseInt(Math.abs(endDate - startDate) / 1000 / 60 / 60 / 24);
	
    var hour = 0;
    if (startDate == endDate) {
        var date = new Date(startDate);
        var week = date.getDay();
        if (week != 0 && week != 6) {
			hour= (endHour-startHour)*10-startMin/6+endMin/6;
			if (startHour <= 12 && endHour>12) {
				hour= hour-15;
			}
        }
    } else {
        for (var i = 0; i <= dayNum; i++) {
            var DateTime = startDate + 60 * 60 * 24 * 1000*i
            var date = new Date(DateTime);
            var week = date.getDay();
            if (week != 0 && week != 6) {
                if (DateTime == startDate) {
					hour+= (18-startHour)*10-(startMin/6);
					if (startHour <= 12) {
						hour= hour-15;
					}
                } else if (DateTime > startDate && DateTime < endDate) {
                    hour += 75;
                } else if (DateTime == endDate){
                    hour+= (endHour-9)*10+endMin/6;
					if (endHour > 12) {
						hour= hour-15;
					}
                }
            }
        }
    }
	if(hour<0){
		hour=0;
	}
    return hour; 
}

const moduleInit = ['tool','employeepicker'];
	function gouguInit() {
		var form = layui.form,
			tool=layui.tool,
			laydate = layui.laydate;
			
		if (typeof (flowStep) == "function") {
			flowStep();
		}	
		//日期时间范围
		lay('.select-time').each(function () {
			laydate.render({
				elem: this,
				trigger: 'click',
				done:function(){
					var hours = leaveTime();
					if(hours == 0){
						$('#hours,#days').html('-');
					}
					else{
						$('#hours').html('共'+hours/10+'小时');
						var days = 0;
						if(hours>=75){
							days = parseInt(hours/75);
						}
						$('#days').html('共'+days+'天，'+(hours%75)/10+'小时');
					}
					$('[name="duration"]').val(hours/10);
				}
			});
		});
		
		//选择具体时间
		lay('.select-datetime').each(function () {
			laydate.render({
				elem: this,
				trigger: 'click',
				type: 'time',
				format: 'HH:mm',
				btns: ['clear', 'confirm'],
				ready: function (date) {
				   var dom = $(".laydate-time-list").children("li");
				   for (var i = 0; i < dom.length; i++) {
					   if (i == 2 || i == 5) {
						   $(dom[i]).remove();
					   } else if(i==1||i==4) {
						   var li = $(dom[i]).children("ol").children("li")
						   for (var j = 0; j < li.length; j++) {
							   if ($(li[j]).text() != 00 && $(li[j]).text()!=30) {
								   $(li[j]).remove();
							   }
						   }
					   }
					}
				},
				done:function(){
					var hours = leaveTime();
					if(hours == 0){
						$('#hours,#days').html('-');
					}
					else{
						$('#hours').html('共'+hours/10+'小时');
						var days = 0;
						if(hours>=75){
							days = parseInt(hours/75);
						}
						$('#days').html('共'+days+'天，'+(hours%75)/10+'小时');
					}
					$('[name="duration"]').val(hours/10);
				}
			});
		});
	}
</script>
{/block}
<!-- /脚本 -->